<?php
	$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
	$objDB = mysql_select_db("db_stock");
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<script language="JavaScript">
	   function ajaxCancel()
		{
			document.getElementById('btnCancel').style.display = 'none';
			document.getElementById("btnUpdate").disabled=true;
		    document.getElementById("btnAdd").disabled=false;
			
			document.getElementById("txtpro_id").value = '';
		    document.getElementById("txtproduct").value = '';
			document.getElementById("txtqty").value = '';
			document.getElementById("txtb_rate").value = '';
			document.getElementById("txts_rate").value = '';
			 document.getElementById("txtavail").value = '';
			document.getElementById("txttotal").value = '';
			
			document.getElementById("btnUpdate").disabled=true;
			document.getElementById("btnAdd").disabled=false;
		}//end cancel
	   var HttPRequest = false;

	   function doCallAjax(Mode) {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
		  var url = 'add_stock_new_action.php';
		  
		  if(Mode == "ADD")
		  {
			  var pmeters = "pd_name=" + encodeURI( document.getElementById("txtproduct").value) +
							"&qty=" + encodeURI( document.getElementById("txtqty").value ) +
							"&b_rate=" + encodeURI( document.getElementById("txtb_rate").value ) +
							"&s_rate=" + encodeURI( document.getElementById("txts_rate").value ) +
							"&avail=" + encodeURI( document.getElementById("txtavail").value ) +
							"&total=" + encodeURI( document.getElementById("txttotal").value ) +
							"&tMode=" + Mode;
			 //}
		  }

		  if(Mode == "UPDATE")
		  {
			  var pmeters = "hpd_name=" + encodeURI( document.getElementById("txthproduct").value) +
			  				"&pd_name=" + encodeURI( document.getElementById("txtproduct").value) +
							"&qty=" + encodeURI( document.getElementById("txtqty").value ) +
							"&b_rate=" + encodeURI( document.getElementById("txtb_rate").value ) +
							"&s_rate=" + encodeURI( document.getElementById("txts_rate").value ) +
							"&avail=" + encodeURI( document.getElementById("txtavail").value ) +
							"&total=" + encodeURI( document.getElementById("txttotal").value ) +
							"&tMode=" + Mode;
		  }

		  if(Mode == "LIST")
		  {
			var pmeters = "";
			document.getElementById("btnUpdate").disabled=true;
			document.getElementById('btnCancel').style.display = 'none';
			document.getElementById('txtpro_id').style.display = 'none';
			document.getElementById("codeName").innerHTML="Product Name";
		  }

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{

				 if(HttPRequest.readyState == 3)  // Loading Request
				  {
				   document.getElementById("mySpan").innerHTML = "Now is Loading...";
				  }

				 if(HttPRequest.readyState == 4) // Return Request
				  {
				   document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
				   document.getElementById("txtpro_id").value = '';
				   document.getElementById("txtproduct").value = '';
				   document.getElementById("txtqty").value = '';
				   document.getElementById("txtb_rate").value = '';
				   document.getElementById("txts_rate").value = '';
				   document.getElementById("txtavail").value = '';
				   document.getElementById("txttotal").value = '';
				   
				   document.getElementById("btnUpdate").disabled=true;
				   document.getElementById("btnAdd").disabled=false;
				   document.getElementById("btnCancel").style.display='none';
				  }
			}
	   }//end fnc_doCallAjax
	   
	   function ajaxUpdateOnChange(Mode,pd_name) {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
		   
							
		  var url = 'add_stock_new_action.php';
		  var pmeters = "pd_name=" + encodeURI( document.getElementById("txtproduct1").value) +
							"&qty=" + encodeURI( document.getElementById("txtqty1").value ) +
							//"&b_rate=" + encodeURI( document.getElementById("txtb_rate").value ) +
							"&s_rate=" + encodeURI( document.getElementById("txts_rate1").value ) +
							"&tMode=" + Mode;
		  

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{

				 if(HttPRequest.readyState == 3)  // Loading Request
				  {
				   document.getElementById("mySpan").innerHTML = "Now is Loading...";
				  }

				 if(HttPRequest.readyState == 4) // Return Request
				  {
				   document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
				   document.getElementById("txtproduct1").value = '';
				   document.getElementById("txtqty1").value = '';
				   //document.getElementById("txtb_rate").value = '';
				   document.getElementById("txts_rate1").value = '';
				  // document.getElementById("txtavail").value = '';
				  // document.getElementById("txttotal").value = '';
				   
				  }
			}

	   }//end ajax Update on change
	   
	   
	   function ajaxDelete(Mode,pd_name) {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
	
		  var url = 'add_stock_new_action.php';
		  var pmeters = "tMode=" + Mode +
						"&tpd_name=" + pd_name;

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{

				 if(HttPRequest.readyState == 3)  // Loading Request
				  {
				   document.getElementById("mySpan").innerHTML = "Now is Loading...";
				  }

				 if(HttPRequest.readyState == 4) // Return Request
				  {
				   document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
				  }
				
			}

	   }//end delete
	   
	   function ajaxAutoFill(pro_id,pro_name,b_rate,s_rate,avail) {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }

		  var url = 'AjaxGetFill.php';
		  var pmeters = "pro_id=" + encodeURI( document.getElementById(pro_id).value);

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{
				if(HttPRequest.readyState == 4) // Return Request
				{
					var myProduct = HttPRequest.responseText;
					if(myProduct != "")
					{
						var myArr = myProduct.split("|");
						document.getElementById(pro_name).value = myArr[0];
						document.getElementById(b_rate).value = myArr[1];
						document.getElementById(s_rate).value = myArr[2];
						document.getElementById(avail).value = myArr[3];
						document.getElementById("txtqty").focus();
					}
				}
				
			}

	   }//end auto fill text
	   
	   //show record to edit
	   function ShowEdit(hpd_name,pd_name,qty,b_rate,s_rate,total,avail,sMode)
	   {
			//document.getElementById("myForm").style.display = '';
			document.getElementById("txthproduct").value = hpd_name;
			document.getElementById("txtproduct").value = pd_name;
			document.getElementById("txtqty").value = qty;
			document.getElementById("txtb_rate").value = b_rate;
			document.getElementById("txts_rate").value = s_rate;
			document.getElementById("txttotal").value = total;
			document.getElementById("txtavail").value = avail;
			var mode= sMode;	
			if(mode=="UPDATE"){
				document.getElementById("btnAdd").disabled=true;
				document.getElementById("btnUpdate").disabled=false;
				document.getElementById('btnCancel').style.display = '';
				document.getElementById('txtCustomerID').disabled = 'true';
			}
	   }
	   
	function calTotal()
	{
		 if(isNaN(document.form1.txtqty.value) || document.form1.txtqty.value == "")
		 {
			//alert('(Number B)Please input Number only.');
			document.form1.txtqty.focus();
			return;	
		 }
		 var qty=parseFloat(document.form1.txtqty.value);
		 var b_rate=parseFloat(document.form1.txtb_rate.value);
		 //var avail=parseFloat(document.form1.txtavail.value);
		 
			 //document.form1.txtqty.value = avail;
			 document.form1.txttotal.value = qty * b_rate;
	}
	function show_text(){
		document.getElementById("txtpro_id").style.display = '';
		document.getElementById("cbopro_id").style.display = 'none';
		document.getElementById("codeName").innerHTML="Product Code";
	}
	function show_cbo(){
		document.getElementById("txtpro_id").style.display = 'none';
		document.getElementById("cbopro_id").style.display = '';
		document.getElementById("codeName").innerHTML="Product Name";
	}
	function redirect(url){
    	window.location = url;
	}
	</script>

<body Onload="JavaScript:doCallAjax('LIST');">
<table width="100%" class="bordered">
   		<tr>
        	<th><img src="images/Card.png" width="48" height="48" /> Please Add Prduct to Carts</th>
        </tr>
         <tr>
             <?php 
				if(isset($_REQUEST['msgStatus']))
					echo " <td><span class='red'>". $_REQUEST['msgStatus'].@$_REQUEST['one']."</span> <img src=\"images/empty_cart.png\" /></td>";
			?>
                           
       </tr>
   </table>
<form name="form1" id="form1" >
	
    <table width="100%" class="bordered">
    	 <thead>
              <tr>
                    <th  colspan="7" align="left">
                        Option:<br> 
                        Product Code:
                        <input type="radio" name="rdpd_code" id="rdpd_code"  value="pd_code" onClick="JavaScript:show_text()"> &nbsp;&nbsp;&nbsp;
                        Product Name:
                        <input type="radio" name="rdpd_code" id="rdpd_name" value="pd_name" onClick="JavaScript:show_cbo();" checked="checked">
                    </th>
              </tr>
        </thead>
              <tr>
                <th> <div align="center" id="codeName">ID or Product</div></th>
                <th > <div align="center">Product</div></th>
                <th > <div align="center">Qty</div></th>
                <th > <div align="center">Buying Price</div></th>
                <th > <div align="center">Sell Rate</div></th>
                <th > <div align="center">Avail</div></th>
                <th> <div align="center">Total</div></th>
              </tr>
        
          <tr>
             <td align="center">
             <input type="text" name="txtpro_id" id="txtpro_id" size="5" onChange="JavaScript:ajaxAutoFill('txtpro_id','txtproduct','txtb_rate','txts_rate','txtavail');" style="width:130px;">
             <select name="cbopro_id" id="cbopro_id" style="width:140px;" onBlur="JavaScript:ajaxAutoFill('cbopro_id','txtproduct','txtb_rate','txts_rate','txtavail');">
                  <option value=""><--Please product--></option>
                     <?php
                        $sql="SELECT * FROM tbl_products ORDER BY pd_id DESC";
                        $result=mysql_query($sql);
                        while($row=mysql_fetch_object($result))
                        {
                     ?>
                  <option value="<?php echo $row->pd_code;?>"><?php echo $row->pd_code ."_" .$row->pd_name;?></option>
                     <?php 
                        }
                    ?>
             </select>
             </td>
            <td><div align="center">
                <input type="hidden" name="txthproduct" id="txthproduct" size="5" ><!-- hiden make condition for update !--> 
                <input type="text" name="txtproduct" id="txtproduct" size="5" style="width:200px;"></div>
            </td>
            <td><input type="text" name="txtqty" id="txtqty" size="20"  style="width:50px;" onKeyUp="JavaScript:calTotal()"></td>
			<td> <input type="text" name="txtb_rate" id="txtb_rate" size="20" style="width:100px;" onKeyUp="JavaScript:calTotal()"></td>           
            <td><div align="center"><input type="text" name="txts_rate" id="txts_rate" size="2" style="width:100px;" ></div></td>
            <td><div align="center"><input type="text" name="txtavail" id="txtavail" size="5" readonly></div></td>
            <td align="right"><input type="text" name="txttotal" id="txttotal" size="5" readonly></td>
          </tr>
    </table>
    <input class="btnSubmit" type="button" name="btnAdd" id="btnAdd" value="ADD"  OnClick="JavaScript:doCallAjax('ADD');" style="width:75px;">
    <input class="btnSubmit" type="button" name="btnUpdate" id="btnUpdate" value="UPDATE"  OnClick="JavaScript:doCallAjax('UPDATE');">
    <input class="btnSubmit" type="button" name="btnCheck" id="btnCheck" value="CHECK OUT"  OnClick="redirect('index.php?page=add_stock&action=add_stock_check_out')">
    <input class="btnSubmit" type="button" name="btnCancel" id="btnCancel" value="CANCEL"  OnClick="JavaScript:ajaxCancel();">
    <br><br>
    <span id="mySpan"></span>
</form>
</body>
</html>